package com.sharding.demo.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

public class OrderDatabaseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Date> shardingValue) {
        Date time = shardingValue.getValue();
        String year = new SimpleDateFormat("YYYY").format(time);
        if("2020".equals(year)){
            return "db0";
        }
        if("2021".equals(year)){
            return "db1";
        }
        if("2022".equals(year)){
            return "db2";
        }
        return null;
    }


}

